886E - Maximum Element - CodeForces Solution


combinatorics dp math *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;
const int P = 1e9 + 7, N = 1e6;

int qpow(int a, int b)
{
    int res = 1;
    for (; b; b >>= 1, a = 1ll * a * a % P)
        if (b & 1) res = 1ll * res * a % P;
    return res;
}

int n, k, ans, fac, sum;
int g[N + 5];

int main()
{
    scanf("%d%d", &n, &k);
    g[0] = fac = 1, ans = n;
    for (int i = 1; i <= n; i++)
    {
        sum = (sum + g[i - 1]) % P;
        if (i > k) sum = (sum - g[i - k - 1] + P) % P;
        g[i] = 1ll * sum * qpow(i, P - 2) % P;
        ans = (ans - g[i - 1] + P) % P;
        if (i < n) fac = 1ll * fac * i % P; 
    }
    printf("%d\n", 1ll * fac * ans % P);
}


Comments

Submit
0 Comments
More Questions

1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo
208D - Prizes Prizes more Prizes